package com.mtsmda.is.admin.domain;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.mtsmda.is.util.Constants;

/**
 * CREATE TABLE [dbo].[Customer]( [customerId] [int] IDENTITY(1,1) NOT NULL,
 * [organisationOrPerson] [bit] NOT NULL, [organisationName] [nvarchar](100)
 * NULL, [gender] [nvarchar](1) NULL, [firstName] [nvarchar](100) NULL,
 * [middleName] [nvarchar](100) NULL, [lastName] [nvarchar](100) NULL,
 * [emailPost] [nvarchar](100) NULL, [phoneNumber] [nvarchar](25) NULL,
 * [mobilePhoneNumber] [nvarchar](25) NULL, [dateOfBirth] [date] NULL,
 * [postIndex] [nvarchar](10) NULL, CONSTRAINT [PK_Customer] PRIMARY KEY
 * CLUSTERED
 * */
public class Customer {
	private Integer customerId;
	private Character organisationOrPerson;
	private String organisationName;
	private Character gender;
	private String firstName;
	private String middleName;
	private String lastName;
	private String emailPost;
	private String phoneNumber;
	private String mobilePhoneNumber;
	private Date dateOfBirth;
	private String postIndex;

	public Customer() {

	}

	public Integer getCustomerId() {
		return customerId;
	}

	public Customer setCustomerId(Integer customerId) {
		this.customerId = customerId;
		return this;
	}

	public Character  getOrganisationOrPerson() {
		return organisationOrPerson;
	}

	public Customer setOrganisationOrPerson(Character  organisationOrPerson) {
		this.organisationOrPerson = organisationOrPerson;
		return this;
	}

	public String getOrganisationName() {
		return organisationName;
	}

	public Customer setOrganisationName(String organisationName) {
		if (organisationName.length() > 100) {
			this.organisationName = organisationName.substring(0, 100);
		} else {
			this.organisationName = organisationName;
		}
		return this;
	}

	public Character getGender() {
		return gender;
	}

	public Customer setGender(Character gender) {
		this.gender = gender;
		return this;
	}

	public String getFirstName() {
		return firstName;
	}

	public Customer setFirstName(String firstName) {
		if (firstName.length() > 100) {
			this.firstName = firstName.substring(0, 100);
		} else {
			this.firstName = firstName;
		}
		return this;
	}

	public String getMiddleName() {
		return middleName;
	}

	public Customer setMiddleName(String middleName) {
		if (middleName.length() > 100) {
			this.middleName = middleName.substring(0, 100);
		} else {
			this.middleName = middleName;
		}
		return this;
	}

	public String getLastName() {
		return lastName;
	}

	public Customer setLastName(String lastName) {
		if (lastName.length() > 100) {
			this.lastName = lastName.substring(0, 100);
		} else {
			this.lastName = lastName;
		}
		return this;
	}

	public String getEmailPost() {
		return emailPost;
	}

	public Customer setEmailPost(String emailPost) {
		if (emailPost.length() > 100) {
			emailPost = emailPost.substring(0, 100);
		}
		this.emailPost = CustomerUtil.checkEmail(emailPost);
		return this;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public Customer setPhoneNumber(String phoneNumber) {
		if (phoneNumber.length() > 25) {
			phoneNumber = phoneNumber.substring(0, 25);
		}
		this.phoneNumber = CustomerUtil.checkPhoneNumber(phoneNumber);
		return this;
	}

	public String getMobilePhoneNumber() {
		return mobilePhoneNumber;
	}

	public Customer setMobilePhoneNumber(String mobilePhoneNumber) {
		if (mobilePhoneNumber.length() > 25) {
			mobilePhoneNumber = mobilePhoneNumber.substring(0, 25);
		}
		this.mobilePhoneNumber = CustomerUtil
				.checkPhoneNumber(mobilePhoneNumber);
		return this;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public Customer setDateOfBirth(Date dateOfBirth) {
		if (dateOfBirth.getTime() <= new Date().getTime()) {
			this.dateOfBirth = dateOfBirth;
		}
		return this;
	}

	public String getPostIndex() {
		return postIndex;
	}

	public Customer setPostIndex(String postIndex) {
		if (postIndex.length() > 10) {
			postIndex = postIndex.substring(0, 10);
		}
		this.postIndex = CustomerUtil.checkPostIndex(postIndex);
		return this;
	}

	@Override
	public String toString() {
		return "Customer [customerId=" + customerId + ", organisationOrPerson="
				+ organisationOrPerson + ", organisationName="
				+ organisationName + ", gender=" + gender + ", firstName="
				+ firstName + ", middleName=" + middleName + ", lastName="
				+ lastName + ", emailPost=" + emailPost + ", phoneNumber="
				+ phoneNumber + ", mobilePhoneNumber=" + mobilePhoneNumber
				+ ", dateOfBirth=" + dateOfBirth + ", postIndex=" + postIndex
				+ "]";
	}

	public interface Gender {
		public static final Character GENDER_M = 'm';
		public static final Character GENDER_F = 'f';
	}
	
	public interface OrganisationOrPerson {
		public static final Character ORGANISATION = 'o';
		public static final Character PERSON = 'p';
	}

	public static class CustomerUtil {

		public static final String PATTERN_PHONE = "^\\d$";

		public static final String PATTERN_EMAIL = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
				+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

		public static final String PATTERN_POST_INDEX = "^[A-Z]{2}[\\d]{4}$";

		public static String checkPhoneNumber(String phoneNumber) {
			if (phoneNumber != null && !phoneNumber.isEmpty()
					&& phoneNumber.length() >= 7) {
				if (applyPattern(PATTERN_PHONE, phoneNumber)) {
					return separateNumberPhone(phoneNumber);
				}
			}
			return Constants.EMPTY;
		}

		public static String checkEmail(String email) {
			if (email != null && !email.isEmpty() && email.length() > 5) {
				if (applyPattern(PATTERN_EMAIL, email)) {
					return email;
				}
			}
			return Constants.EMPTY;
		}

		public static String checkPostIndex(String postIndex) {
			if (postIndex != null && !postIndex.isEmpty()
					&& postIndex.length() > 5) {
				if (applyPattern(PATTERN_POST_INDEX, postIndex)) {
					return postIndex.substring(0, 2) + "-"
							+ postIndex.substring(2, postIndex.length());
				}
			}
			return Constants.EMPTY;
		}

		private static boolean applyPattern(String patternS, String value) {
			Pattern pattern = Pattern.compile(patternS);
			Matcher matcher = pattern.matcher(value);
			return matcher.find();
		}

		private static String separateNumberPhone(String numberPhone) {
			String result = new String("");
			if (numberPhone.length() > 0) {
				for (int i = 0; i < numberPhone.length(); i = i + 3) {
					if ((i + 3) < numberPhone.length()) {
						result += numberPhone.substring(i, i + 3) + "-";
					} else {
						result += numberPhone
								.substring(i, numberPhone.length());
					}
				}
			}
			return result;
		}
	}

}